
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Container;
using Swaf.Security;


namespace eCom
{
	public class EComTemplateDisplayBizRules : Syrinx.BaseBizRules
	{
		public string getEComStatusDisplayTemplate(IApplication app, IRuleContext context, IBizObj order, Privileges editPriv, string parentBPIds, string pageName)
		{
			if (order != null && ((IBizObjList)order.get("items")).numElements > 0)
				return "eCom.Gui.AspNet.SalesOrderArticleStatusDisplay";
			return "";
		}
		public string getEComAccountSettingsTemplate(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			string template = "eCom.Gui.AspNet.eComAccountSettingsDisplay";
			if (resultValue != null)
				return resultValue.ToString().Length == 0 ? template
					: new StringBuilder(50).Append(resultValue.ToString()).Append(",").Append(template).ToString();
			return template;
		}
		public IBizObjList getEComAccountSettingsDbFieldNames(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			IBizObjList settingsFields = resultValue as IBizObjList;

			IBizObj eca = settingsFields.insertNew();
			eca.put("dbName", "eComAccountId");
			eca.put("bizObjName", "eComAccountId");
			eca.put("aliasName", "eComAccountId");
			eca.put("dbType", "UniqueIdentifier");
			
			IBizObj ueca = settingsFields.insertNew();
			ueca.put("dbName", "postToUserEComAccount");
			ueca.put("bizObjName", "postToUserEComAccount");
			ueca.put("aliasName", "postToUserEComAccount");
			ueca.put("dbType", "Boolean");

			IBizObj usm = settingsFields.insertNew();
			usm.put("dbName", "defaultUserMembershipProductPartId");
			usm.put("bizObjName", "defaultUserMembershipProductPartId");
			usm.put("aliasName", "defaultUserMembershipProductPartId");
			usm.put("dbType", "UniCodeVarChar");

			usm = settingsFields.insertNew();
			usm.put("dbName", "defaultCreateNewUser");
			usm.put("bizObjName", "defaultCreateNewUser");
			usm.put("aliasName", "defaultCreateNewUser");
			usm.put("dbType", "UniCodeVarChar");

			usm = settingsFields.insertNew();
			usm.put("dbName", "priceCurrencyPrefix");
			usm.put("bizObjName", "priceCurrencyPrefix");
			usm.put("aliasName", "priceCurrencyPrefix");
			usm.put("dbType", "UniCodeVarChar");
			
			usm = settingsFields.insertNew();
			usm.put("dbName", "eComMaxNumProducts");
			usm.put("bizObjName", "eComMaxNumProducts");
			usm.put("aliasName", "eComMaxNumProducts");
			usm.put("dbType", "Int");
			
			return settingsFields;
		}

		public IBizObj setupEComCompanySettings(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			IBizObj company = resultValue as IBizObj;
			if (company != null)
			{
				context.put("companyId", company.get("id"));
				IBizObj count = app.appData.get("DA.eCom.GetCountOfCompanyPaymentTerms", context) as IBizObj;
				if (count != null && (int)count.get("count") > 0)
					company.put("settings.hasPaymentTerms", true);
			}
			return company;
		}

		public string getProductSalesDisplayTemplate(IApplication app, IRuleContext context, IBizObj product, Privileges editPriv, string parentBPIds, string pageName)
		{
			try
			{
				string rwTemplate = "Articles.Gui.AspNet.Article-RWSimpleDisplay,eCom.Gui.AspNet.ProductDetailsRWView";
				if (product != null)
				{
					ProductMetaType mt = (ProductMetaType)Enum.Parse(typeof(ProductMetaType), product.get("metaType").ToString());
					switch (mt)
					{
						case ProductMetaType.Simple:
							return editPriv == Privileges.ReadOnly ? "eCom.Gui.AspNet.ProductSalesItemDisplay" : rwTemplate;
						case ProductMetaType.Bundled:
							return editPriv == Privileges.ReadOnly ? "eCom.Gui.AspNet.ProductBundledSalesItemDisplay" : rwTemplate;
						case ProductMetaType.WithChild:
							return editPriv == Privileges.ReadOnly ? "eCom.Gui.AspNet.ProductWithChildProductDisplay" : rwTemplate;
					}
				}
				return "eCom.Gui.AspNet.ProductSalesItemDisplay";
			}
			catch (Exception e)
			{
				throw new eComException("error executing getProductSalesDisplayTemplate.", e);
			}
		}

		public string getCheckOutDisplayTemplate(IApplication app, IRuleContext context, IBizObj order, Privileges editPriv
			, string parentBPIds, string pageName)
		{
			if (order != null && Convert.ToDecimal(order.get("grandTotal")) > 0)
				return "eCom.Gui.AspNet.BasicCartStatusDisplay";
			return "";

		}

		public string getPaymentMethodDisplayTemplate(IApplication app, IRuleContext context, IBizObj paymentMethod,
			Privileges editPriv, string parentBPIds, string pageName)
		{
			string template = "";

			if (paymentMethod != null)
			{

				string bizObjType = paymentMethod.type.Name;
				if (bizObjType != "eComPaymentTerm")
					template = new StringBuilder(25).Append("eCom.Gui.AspNet.").Append(bizObjType).Append("Display").ToString();
				else
					template = "eCom.Gui.AspNet.eComNoPaymentInfoDisplay";
			}

			return template;
		}

		public object getProductsBackupRestoreTemplate(IApplication app, IRuleContext context, object resultValue, object[] inputParams)
		{
			string template = "eCom.Gui.AspNet.BaseDeploymentOptions";
			if (resultValue != null)
				return resultValue.ToString().Length == 0 ? template
					: new StringBuilder(50).Append(resultValue.ToString()).Append(",").Append(template).ToString();
			return template;
		}

	}
}
